package org.bud.util.bit;

import org.bud.util.constant.Symbol;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.IntStream;

/**
 * @author shaun
 * @date 2024/7/18
 * @instruction
 */
public abstract class Maps {
    public static <K,V> boolean isNotEmpty(Map<K,V> map){
        return map != null && map.keySet().size() > 0;
    }

    public static <K,V> boolean isEmpty(Map<K,V> map){
        return map == null || map.keySet().size() == 0;
    }

    public static <K,V> Map<K,V> newHashMap(K[] k,V[] v){
        return map(HashMap::new,k,v);
    }

    public static <K,V> Map<K,V> map(Supplier<Map<K,V>> supplier,K[] k, V[] v){
        return IntStream.rangeClosed(0,k.length - 1)
                .collect(supplier,(m,i) -> m.put(k[i],v[i]),Map::putAll);
    }

    public static Map<String,Object> newHashMap(Object... value){
        return map(HashMap::new,value);
    }

    public static Map<String,Object> map(Supplier<Map<String,Object>> supplier,Object... value){
        return IntStream.rangeClosed(0,value.length / 2 - 1)
                .collect(supplier,(m,i) ->
                        m.put(Optional.ofNullable(value[2 * i])
                                .map(Object::toString)
                                .orElse(Symbol.BLANK),
                                value[2 * i + 1]
                        ),
                        Map::putAll
                );
    }
}
